package nut.system;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
/**
 * Factory for the package.
 * 
 * @category stable
 * @author Mark Farnsworth
 */
class NutFactory {
	static NutFactory the = new NutFactory();
	final Map<String, NutVersion> bags = new HashMap<String, NutVersion>();
	Map<String, NutCategory> categories = new HashMap<String, NutCategory>();
	public final DateFormat dateFormat = new SimpleDateFormat("yy-mm-dd");
	Map<String, NutAuthor> farmers = new HashMap<String, NutAuthor>();
	private int nextId;
	Map<String, NutItem> nuts = new HashMap<String, NutItem>();
	Map<String, NutItem> roots = new HashMap<String, NutItem>();
	NutVersion bag(final String name) {
		NutVersion n = bags.get(name);
		if (n == null) {
			n = new NutVersion(name);
			bags.put(name, n);
		}
		return n;
	}
	NutCategory category(final String name) {
		NutCategory n = categories.get(name);
		if (n == null) {
			n = new NutCategory(name);
			categories.put(name, n);
		}
		return n;
	}
	/**
	 * @category alpha
	 * @param name
	 * @return
	 */
	NutAuthor farmer(final String name) {
		NutAuthor n = farmers.get(name);
		if (n == null) {
			n = new NutAuthor(name);
			farmers.put(name, n);
		}
		return n;
	}
	Integer nextId() {
		return Integer.valueOf(nextId++);
	}
	NutItem nut(final String name) {
		NutItem n = nuts.get(name);
		if (n == null) {
			n = new NutItem(name);
			nuts.put(name, n);
		}
		return n;
	}
	NutItem root(String name) {
		final String prefix = "Nut";
		if (name.startsWith(prefix))
			name = name.substring(prefix.length());
		NutItem n = roots.get(name);
		if (n == null) {
			n = new NutItem(name);
			roots.put(name, n);
		}
		return n;
	}
}
